package cn.ekgc.system.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    // 实现redis 增删改查的对象
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    
    // 往redis 赋值
    public void putValue(String key,String value){
        // 获取操作值的对象
        ValueOperations<String,String> valueOperations = stringRedisTemplate.opsForValue();
        valueOperations.set(key,value);
        System.out.println("redis存储成功" +"key为"+ key +"value为"+value);
    }

    public void putValueByTime(String key,String value,long time){
        // 获取操作值的对象
        ValueOperations<String,String> valueOperations = stringRedisTemplate.opsForValue();

        // 传入时间参数    单位为秒
        valueOperations.set(key,value,time, TimeUnit.SECONDS);
        System.out.println("redis存储成功" +"key为"+ key +"value为"+value);
    }

    // 获取值
    public String getValue(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    // 删除值
    public void delValue(String key){
         stringRedisTemplate.delete(key);
    }

    // 指定key更新失效时间
    public void updateExpireTime(String key,long time){
        // 更新秒数
        stringRedisTemplate.expire(key,time,TimeUnit.SECONDS);
    }
}
